package com.yisu.expressway.customService.adapter;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yisu.expressway.R;
import com.yisu.expressway.customService.VoiceAssistantActivity;
import com.yisu.expressway.customService.model.HighWayInfoObj;
import com.yisu.expressway.databinding.TextWithArrowForDatabindingBinding;
import com.yisu.expressway.listener.onClickHandler;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/1/2 0002.
 */

public class VoiceListItemAdapter extends RecyclerView.Adapter<VoiceListItemAdapter.ListItemViewHolder> {

    @NonNull
    private ArrayList<HighWayInfoObj> mData = new ArrayList<>();
    VoiceAssistantActivity mActivity;

    public VoiceListItemAdapter(VoiceAssistantActivity activity){
        mActivity = activity;
    }

    public void update (ArrayList<HighWayInfoObj> list){
        mData = list;
        notifyDataSetChanged();
    }

    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.text_with_arrow_for_databinding, parent, false);
        return new ListItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ListItemViewHolder holder, int position) {
        holder.bind(mData.get(position));
    }

    public  class ListItemViewHolder extends RecyclerView.ViewHolder implements onClickHandler {
        private TextWithArrowForDatabindingBinding itemBinding;

        public ListItemViewHolder(View view){
            super(view);
            itemBinding = DataBindingUtil.bind(view);
        }

        public void bind(@NonNull HighWayInfoObj questionContent){
            itemBinding.setSeleteItem(questionContent);
//            itemBinding.setItemContent(questionContent.answer);
            itemBinding.setClickListener(this);
        }

        @Override
        public void handleClick(View view) {
            HighWayInfoObj selectItem = itemBinding.getSeleteItem();
            if(selectItem!= null) {
                if(selectItem.isHaveSubordinate == 1) {
                    mActivity.getHighWayAnswerFromServer(selectItem.levelId,selectItem.question);
                }else {
                    mActivity.getHighWayAnswerFromItem(selectItem);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }
}
